﻿using DevTeam;
using pvdcd;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Controls;
using System.Windows.Markup;
using Template;
using XO1;

namespace Karbel
{
    public class Data
    {
        public static DataManager DM = new DataManager("Server=karbelerp.karbel.com,1430;Database=ConfExpert;User Id=CEUser;Password=1034!1518;");

        public static void AddNewPart()
        {
            DM.ExecuteDataTableByQuery("insert into CAD_ParcaTipi(ParcaAdi,OnArka,SolSag) values ('Yeni Parça',1,1)");
        }

        public static void AddTemplateParts(Template.ReferanceTemplate template)
        {
            DataTable dt = DM.ExecuteDataTableByQuery("select * from CAD_ParcaTipi");

            foreach (DataRow dr in dt.Rows)
            {
                string name = (string)dr["ParcaAdi"];
                bool onArka = (bool)dr["OnArka"];
                bool solSag = (bool)dr["SolSag"];
                Template.Type T = new Template.Type(name, onArka, solSag);
                template.PartTypeList.Add(T);
            }
        }

        public static void RemovePart(string ParcaTipi)
        {
            DM.ExecuteDataTableByQuery("delete from CAD_ParcaTipi where ParcaAdi = '" + ParcaTipi + "'");
        }

        public static void RenamePart(string first, string last)
        {
            DM.ExecuteDataTableByQuery("update CAD_ParcaTipi set ParcaAdi = '" + last + "' where ParcaAdi = '" + first + "'");
        }

        public static DataTable Sablonlar
        {
            get
            {
                return DM.ExecuteDataTableByQuery("SELECT * FROM CAD_Sablon");
            }
        }

        public static DataTable Parcalar
        {
            get
            {
                return DM.ExecuteDataTableByQuery("SELECT * FROM CAD_Parca");
            }
        }

        public static void SaveTemplate(Template.ReferanceTemplate TemplateX)
        {
            MemoryStream sablon = new MemoryStream();
            new BinaryFormatter().Serialize(sablon, TemplateX);

            MemoryStream on = new MemoryStream();
            XamlWriter.Save(TemplateX.ModelOn, on);

            MemoryStream arka = new MemoryStream();
            XamlWriter.Save(TemplateX.ModelArka, arka);

            DM.ExecuteDataTable("CAD_InsSablon", TemplateX.Name, TemplateX.PreparedBy, sablon.ToArray(), on.ToArray(), arka.ToArray());
        }


        public static void SavePart(pvdcd.Part part)
        {
            string sablonAdi = part.TemplateName;
            string parcaTuru = part.PartType.PartType;


            MemoryStream msPart = new MemoryStream();

            MemoryStream msOnSol = new MemoryStream();
            MemoryStream msOnSag = new MemoryStream();

            MemoryStream msArkaSol = new MemoryStream();
            MemoryStream msArkaSag = new MemoryStream();

            new BinaryFormatter().Serialize(msPart, part);
            XamlWriter.Save(part.FrontLeftView, msOnSol);

            MemoryStream msDetail0 = new MemoryStream();
            MemoryStream msDetail1 = new MemoryStream();
            MemoryStream msDetail2 = new MemoryStream();

            for (int i = 0; i < part.DetailViews.Count; i++)
            {
                DetailView detailView = part.DetailViews[i];

                if (i == 0)
                    XamlWriter.Save(detailView.Detail, msDetail0);
                else if (i == 1)
                    XamlWriter.Save(detailView.Detail, msDetail1);
                else if (i == 2)
                    XamlWriter.Save(detailView.Detail, msDetail2);
            }

            if (part.PartType.FrontBack && part.PartType.LeftRight)
            {
                XamlWriter.Save(part.FrontRightView, msOnSag);
                XamlWriter.Save(part.BackLeftView, msArkaSol);
                XamlWriter.Save(part.BackRightView, msArkaSag);
            }
            else if (part.PartType.FrontBack && !part.PartType.LeftRight)
            {
                XamlWriter.Save(part.BackLeftView, msArkaSol);
            }
            else
            {
                if (part.PartType.FrontBack || !part.PartType.LeftRight)
                    return;

                XamlWriter.Save(part.FrontRightView, msOnSag);
            }

            DM.ExecuteDataTable("CAD_InsParca", part.PartName, msPart.ToArray(), msOnSol.ToArray(), msOnSag.ToArray(), msArkaSol.ToArray(), msArkaSag.ToArray(), msDetail0.ToArray(), msDetail1.ToArray(), msDetail2.ToArray());
        }

        public static List<ReferanceTemplate> GetSablonlar()
        {
            DataTable dt = Karbel.Data.Sablonlar;
            List<ReferanceTemplate> list = new List<ReferanceTemplate>();

            foreach (DataRow dr in dt.Rows)
            {
                ReferanceTemplate Sablon = GetSablon(dr);
                list.Add(Sablon);
            }

            return list;
        }

        public static ReferanceTemplate GetSablon(DataRow dr)
        {
            byte[] sablon = (byte[])dr["Sablon"];
            string xamlOn = dr["On"].ToString();
            string xamlArka = dr["Arka"].ToString();

            MemoryStream msSablon = new MemoryStream(sablon);
            ReferanceTemplate Sablon = (ReferanceTemplate)new BinaryFormatter().Deserialize(msSablon);
            Sablon.DriveTemplate();

            Canvas cOn = XamlFileOperations.newOpenXaml(xamlOn);
            Canvas cArka = XamlFileOperations.newOpenXaml(xamlArka);
            Sablon.ModelOn = cOn;
            Sablon.ModelArka = cArka;
            return Sablon;
        }


        public static Part GetParca(DataRow dr)
        {
            byte[] parca = (byte[])dr["Parca"];

            string xamlOnSol = dr["OnSol"].ToString();
            string xamlOnSag = dr["OnSag"].ToString();
            string xamlArkaSol = dr["ArkaSol"].ToString();
            string xamlArkaSag = dr["ArkaSag"].ToString();

            string xamlDetay0 = dr["Detay0"].ToString();
            string xamlDetay1 = dr["Detay1"].ToString();
            string xamlDetay2 = dr["Detay2"].ToString();


            Part part = (Part)new BinaryFormatter().Deserialize(new MemoryStream(parca));
            part.DrivePart();

            if (xamlOnSol != "")
                part.FrontLeftView = XamlFileOperations.newOpenXaml(xamlOnSol);

            if (xamlOnSag != "")
                part.FrontRightView = XamlFileOperations.newOpenXaml(xamlOnSag);

            if (xamlArkaSol != "")
                part.BackLeftView = XamlFileOperations.newOpenXaml(xamlArkaSol);

            if (xamlArkaSag != "")
                part.BackRightView = XamlFileOperations.newOpenXaml(xamlArkaSag);

            if (xamlDetay0 != "")
                part.DetailViews[0].Detail = XamlFileOperations.newOpenXaml(xamlDetay0);

            if (xamlDetay1 != "")
                part.DetailViews[1].Detail = XamlFileOperations.newOpenXaml(xamlDetay1);

            if (xamlDetay2 != "")
                part.DetailViews[2].Detail = XamlFileOperations.newOpenXaml(xamlDetay2);

            return part;
        }

        public static List<Part> GetParcalar()
        {
            List<Part> parts = new List<Part>();
            DataTable dt = Data.DM.ExecuteDataTableByQuery("select * from CAD_Parca");
            foreach (DataRow dr in dt.Rows)
                parts.Add(GetParca(dr));

            return parts;
        }
    }
}
